package com.jingyanzi.finance.util;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;


import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.jingyanzi.finance.factory.FinanceFactory;

public class ExportUtil {
	
	private String tempatePath = null;
	
	private String saveDocPath = null; 
	
	private final static String BILL_TEMP_PROPERTY_KEY = "bill.template.path";
	
	private final static String CHECK_TEMP_PROPERTY_KEY = "check.template.path";
	
	private final static String DOC_SAVE_PROPERTY_KEY = "doc.save.path";
	
	private final static int BILL = 1;
	
	private final static int CHECK = 2;
	
	public static String[] pdfBillKey = { "voucherNO", "businessNO", "name", "tel", "store", "salse", "orderNO",
			"billTypeName", "amount", "payAmount", "cashierName", "place", "cashierDate" };
	
	private int docType = 1;
	
	private FileOutputStream out = null;
	
	private ByteArrayOutputStream bos = null;

	public String getTempatePath() {
		return tempatePath;
	}


	public String getSaveDocPath() {
		return saveDocPath;
	}


	public static ExportUtil exportFactory(int doc) {
		
		ExportUtil exportUtil = new ExportUtil();
		
		Properties properties = new Properties();
		InputStream in = FinanceFactory.class.getClassLoader().getResourceAsStream("application.properties");
		try {
			properties.load(in);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		switch(doc) {
			case BILL: {
				if (properties.getProperty(BILL_TEMP_PROPERTY_KEY) != null) {
					exportUtil.tempatePath = properties.getProperty(BILL_TEMP_PROPERTY_KEY);
				} else {
					exportUtil.tempatePath = "d:/bill.pdf";
				}
				exportUtil.docType = BILL;
				break;
			}
			case CHECK: {
				if (properties.getProperty(CHECK_TEMP_PROPERTY_KEY) != null) {
					exportUtil.tempatePath = properties.getProperty(CHECK_TEMP_PROPERTY_KEY);
				} else {
					exportUtil.tempatePath = "d:/check.xls";
				}
				exportUtil.docType = CHECK;
				break;
			}
		}
		
		if (properties.getProperty(DOC_SAVE_PROPERTY_KEY) != null) {
			exportUtil.saveDocPath = properties.getProperty(DOC_SAVE_PROPERTY_KEY);
		}
		return exportUtil;
	}
	
	public boolean writer(Map<String, Object> items) {
		if(docType == BILL) {
			return billWriter(items);
		} else if(docType == CHECK){
			return checkWriter(items);
		}
		return false;
	}
	
	public boolean billWriter(Map<String, Object> items) {
		
		Lock lock = new ReentrantLock(); 
		
		PdfReader reader = null;
		
		PdfStamper stamper = null;
		
		Document doc = null;
		
		try {
			saveDocPath = saveDocPath + items.get("voucherNO") + ".pdf";
			
			out = new FileOutputStream(saveDocPath);// 输出流
			reader = new PdfReader(tempatePath);// 读取pdf模板
			bos = new ByteArrayOutputStream();
			stamper = new PdfStamper(reader, bos);
		
			AcroFields form = stamper.getAcroFields();
			for (String key : pdfBillKey) {
				if(items.get(key) != null) {
					form.setField(key, items.get(key).toString());
				}
			}
			stamper.setFormFlattening(true);// 如果为false那么生成的PDF文件还能编辑，一定要设为true
			stamper.close();
			lock.lock();
			doc = new Document();
			PdfCopy copy = new PdfCopy(doc, out);
			doc.open();
			PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), 1);
			copy.addPage(importPage);

		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			doc.close();
			lock.unlock();
			try {
				if (out != null) {
					out.close();
				}
				if (bos != null) {
					bos.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
				return false;
			}
			
			reader.close();
		}
		return true;
	}
	
	public boolean checkWriter(Map<String, Object> items) {
		return true;
	}
	
}
